using System;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using TShooter.TfsWord.Resources;
using TShooter.TfsWord.Win.WizardSupport.Properties;

namespace TShooter.TfsWord.Win.WizardSupport.Design
{
    public class WizardPageDesigner : ParentControlDesigner
    {
        public override DesignerVerbCollection Verbs
        {
            get
            {
                var verbs = new DesignerVerbCollection {new DesignerVerb(Strings.RemovePage, Verb_RemovePage)};

                return verbs;
            }
        }

        private void Verb_RemovePage(object sender, EventArgs e)
        {
            var page = Control as WizardPage;

            if (page == null) return;

            var designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
            var componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
            var designerTransaction = designerHost.CreateTransaction(Strings.RemovePage);

            if (page.Parent is Wizard)
            {
                var wizard = page.Parent as Wizard;

                if (wizard != null)
                {
                    componentChangeService.OnComponentChanging(wizard, null);
                    
                    //Drop from wizard
                        
                    wizard.Pages.Remove(page);
                    wizard.Controls.Remove(page);
                    componentChangeService.OnComponentChanged(wizard, null, null, null);
                }

                designerHost.DestroyComponent(page);
            }
            else
            {
                componentChangeService.OnComponentChanging(page, null);
                    
                //Mark for destruction
                    
                page.Dispose();
                componentChangeService.OnComponentChanged(page, null, null, null);
            }
            designerTransaction.Commit();
        }
    }
}